window.LOCAL_KEY = 'todomvc'

/**
 * 返回任务列表
 */
export function fetchTodoList() {
  const result = localStorage.getItem(LOCAL_KEY)
  if (result) {
    return JSON.parse(result)
  }
  return []
}

/**
 * 设置人物列表
 */
export function setTodoList(todoList) {
  const result = JSON.stringify(todoList)
  localStorage.setItem(LOCAL_KEY, result)
}

/**
 * 获取任务id
 */
export function getTodoId() {
  return Date.now() + Math.random().toString(16).substring(2, 6)
}

/**
 * 根据键值对任务进行筛选
 */
export function filterTodolist(todoList, visiblity = 'all') {
  if (visiblity === 'all') {
    return todoList
  } else if (visiblity === 'active') {
    return todoList.filter(item => !item.isFinish)
  } else if (visiblity === 'completed') {
    return todoList.filter(item => item.isFinish)
  }
  throw new Error('visiblity错误')
}